﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using TaskKernel.Configuration;
using TaskKernel.DTO;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IPersonTasksView : IAbstractView
    {
        IList<ExecutorsModel> Executors { set; }
        IList<ExecutorsModel> IamParticipantAndOwner { get; set; }
    }

    public partial class PersonTasksView : ViewPage<IPersonTasksView, PersonTasksPresenter>, IPersonTasksView
    {
        private const int _itemIndex = 0;

        public IList<TaskListInDepartmentModel> TasksList
        {
            set
            {
                var control = ((GridView) (ExecutorsList.Items[_itemIndex].FindControl("TasksListGridView")));
                control.DataSource = value;
                control.DataBind();
            }
        }

        public bool SortDirection
        {
            get { return ViewState["sortDirectionASC"] != null ? (bool) ViewState["sortDirectionASC"] : true; }
            set { ViewState["sortDirectionASC"] = value; }
        }

        #region IPersonTasksView Members

        public string DepartmentName
        {
            set { Department.Text = value; }
        }

        public IList<ExecutorsModel> Executors
        {
            set
            {
                ViewState["TasksList"] = value;
                ExecutorsList.DataSource = value;
                ExecutorsList.DataBind();
            }
            get { return (IList<ExecutorsModel>) ViewState["TasksList"]; }
        }

        public IList<ExecutorsModel> IamParticipantAndOwner
        {
            set
            {
                ViewState["IamParticipantAndOwner"] = value;
                DataList1.DataSource = value;
                DataList1.DataBind();
            }
            get { return (IList<ExecutorsModel>) ViewState["IamParticipantAndOwner"]; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Presenter.OnViewInitialized();
                var queue = ReadSortQueueFromCookie();
                SortExecutors(queue);
                SortIamParticipantAndOwner(queue);
            }
        }

        private void SortExecutors(Queue<AbstractSortField> queue)
        {
            if (queue.Count > 0)
                Executors = Executors.Select(x => x.SortByMany(new Queue<AbstractSortField>(queue))).ToList();
        }

        private void SortIamParticipantAndOwner(Queue<AbstractSortField> queue)
        {
            if (queue.Count > 0)
                IamParticipantAndOwner =
                    IamParticipantAndOwner.Select(x => x.SortByMany(new Queue<AbstractSortField>(queue))).ToList();
        }

        private Queue<AbstractSortField> ReadSortQueueFromCookie()
        {
            string sortingFields = CookieHelper.ReadCookie(TaskKernelConfiguration.PersonPage + "ComplexSort");
            return sortingFields.ToQueue();
        }

        protected void ExecutorsList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Fill(e, Executors);
        }

        private void Fill(DataListItemEventArgs e, IList<ExecutorsModel> l)
        {
            int elementInExecutorsList = e.Item.ItemIndex;
            var gridViewForTaskList = ((GridView) (e.Item.FindControl("TasksListGridView")));
            gridViewForTaskList.DataSource = l[elementInExecutorsList].TaskList;
            gridViewForTaskList.DataBind();
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Fill(e, IamParticipantAndOwner);
        }

        protected void TasksListGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            Executors = Executors.Select(x => x.Sort(e.SortExpression, SortDirection)).ToList();
            IamParticipantAndOwner =
                IamParticipantAndOwner.Select(x => x.Sort(e.SortExpression, SortDirection)).ToList();
            SortDirection = !SortDirection;
        }
    }
}